package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @ResponseBody
    @RequestMapping("selectAllUser")
    public Map selectAllUser(Integer gid,Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<User> pageInfo = userService.selectAllUser(gid, page, limit);
        map.put("msg","");
        map.put("code",0);
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @ResponseBody
    @RequestMapping("selectOneUser")
    public User selectOneUser(Integer id){
        User user = userService.selectOneUser(id);
        return user;
    }
    @ResponseBody
    @RequestMapping("insertUser")
    public Map insertUser(User user){
        Map map = new HashMap();
        System.out.println("user = " + user);
        Boolean isAdd = userService.insertUser(user);
        map.put("isAdd",isAdd);
        return map;
    }
    @ResponseBody
    @RequestMapping("updateUser")
    public Map updateUser(User user){
        Map map = new HashMap();
        System.out.println("userUpdate = " + user);
        Boolean isUpdate = userService.updateUser(user);
        map.put("isUpdate",isUpdate);
        return map;
    }
    @ResponseBody
    @RequestMapping("deleteUser")
    public Map deleteUser(User user){
        Map map = new HashMap();
        if(user.getStatus()!=3){
            user.setStatus(3);
        }
        System.out.println("user = " + user);
        Boolean isDelete = userService.updateUser(user);
        map.put("isDelete",isDelete);
        return map;
    }
    @ResponseBody
    @RequestMapping("selectCount")
    public Map selectCount(){
        Map map = new HashMap();
        List<User> users = userService.selectCount();
        map.put("users",users);
        return map;
    }
    @ResponseBody
    @RequestMapping("selectWeek")
    public Map selectWeek() throws InterruptedException {
        Map map = new HashMap();
        Integer one = 0;
        Integer two = 1;
        Integer there = 2;
//        long start = System.currentTimeMillis();
//        Map map = userService.selectCommon();
//        long end = System.currentTimeMillis();
//        System.out.println(end-start+"msaaaa");
        List<Map> map1 = userService.selectWeek(one);
        List<Map> map2 = userService.selectWeek(two);
        List<Map> map3 = userService.selectWeek(there);
        map.put("map1",map1);
        map.put("map2",map2);
        map.put("map3",map3);
        return map;
    }
    @ResponseBody
    @RequestMapping("selectChina")
    public List<Map> selectChina(){
        Map map = new HashMap();
        List<Map> maps = userService.selectChina();
        System.out.println("maps = " + maps);
        return maps;
    }
    @ResponseBody
    @RequestMapping("selectAll")
    public Map selectAll() throws InterruptedException {
        //开始时间
        long start = System.currentTimeMillis();
        Map map = userService.selectCommon();
        //结束时间
        long end = System.currentTimeMillis();
        //计算方法执行时间
        System.out.println(end-start+"msaaaa");
        System.out.println("map = " + map);
        return map;
    }
}
